package org.libarae.communication.request;

import java.io.ByteArrayOutputStream;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.libarae.communication.HTTPUtils;
import org.libarae.communication.XmlParseException;
import org.libarae.communication.common.Type;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class BookInfoRequest extends Request {
	private int mBid;
	
	public BookInfoRequest(){
		super(Type.BOOK_INFO);
	}

	public BookInfoRequest(int bid){
		super(Type.BOOK_INFO);
		this.mBid = bid;
	}
	
	@Override
	public String createXML() throws XmlParseException {
		Document doc;
		try{
			//setup the factory to build the xml
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			doc = documentBuilder.newDocument();
		}
		catch (Exception e) {
			throw new XmlParseException("Error creating XML Builder",e);
		}
		
		//create the xml
		Element rootElement = doc.createElement("type");
		rootElement.setAttribute("typeID", super.getType()+"");
		doc.appendChild(rootElement);
		//set the accessKey
		Element node = doc.createElement("mAccessKey");
		node.setTextContent(getAccessKey());
		rootElement.appendChild(node);
		
		node = doc.createElement("mBid");
		node.setTextContent(mBid+"");
		rootElement.appendChild(node);
		
		//export the xml to a string
		String output = null;
		try {
			ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(byteArrayOutput);
			transformer.transform(source, result);
			output = byteArrayOutput.toString();
		}
		catch (Exception e) {
			throw new XmlParseException("Error outputting XML to string", e);
		}
		
		//return the string
		return output;
	}

	@Override
	public void decodeXML(Document doc) {
		// get the document root and node list
		Element root = doc.getDocumentElement();
		NodeList nodeList = root.getChildNodes();
		
		//set type
		super.setType(Type.BOOK_INFO);

		// set all of the nodes in their proper places
		for (int i = 0; i < nodeList.getLength(); i++) {
			// find what node we have
			String nodeName = nodeList.item(i).getNodeName();
			if (nodeName.equals("mAccessKey")) {
				setAccessKey(nodeList.item(i).getTextContent());
			}
			else if (nodeName.equals("mBid")) {
				mBid = Integer.parseInt(nodeList.item(i).getTextContent());
			}			
			else {
				System.err.println("Unknow field in object XML: " + nodeName);
			}
		}
	}

	/**
	 * @return the mBid
	 */
	public int getBid() {
		return mBid;
	}

	/**
	 * @param mBid the mBid to set
	 */
	public void setBid(int mBid) {
		this.mBid = mBid;
	}

	@Override
	public String creatGET() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void decodeGET(String postMessage) {
		Map<String, String> map = HTTPUtils.getParams(postMessage);
		
		setBid(Integer.parseInt(map.get("mBid")));
		
	}
}